const formValidateFun = (value, type) => {
  value = (value + '').replace(/(^\s*)|(\s*$)/g, ''); // 先去除前后空格，排除都是空格的情况
  switch (type) {
    case 'number':
      value = value || value === 0 ? Number(value) : '';
      break;
  }
  return value;
};
const validate = {
  //必填
  required: (trigger = 'blur') => {
    return [
      {
        required: true,
        message: '不能为空！',
        trigger: [trigger],
      },
    ];
  },
  //数组必填
  arrayRequired: () => {
    return [
      {
        validator: (rule, value, callback) => {
          if (value.length === 0) {
            callback(new Error('不能为空！'));
          } else {
            callback();
          }
        },
        trigger: 'blur',
      },
    ];
  },
  //填写字符长度
  maxLength: (required = false, min = 0, max = 255) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        min: min,
        message: `不能小于${min}位字符！`,
        trigger: 'blur',
      },
      {
        max: max,
        message: `不能大于${max}位字符！`,
        trigger: 'blur',
      },
    ];
  },
  //数字
  number: (required = false) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        pattern: /^[0-9.]*$/,
        message: '必须为数字',
        trigger: 'blur',
      },
    ];
  },
  // 数字范围
  numRange: (required = false, min = 0, max = 999) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        min: min,
        max: max,
        type: 'number',
        message: `数字需在${min}-${max}之间`,
        transform: (value) => formValidateFun(value, 'number'),
      },
    ];
  },
  //小数
  decimal: (required = false, num = 2) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        pattern: /^[0-9.]*$/,
        message: '必须为数字',
        trigger: 'blur',
      },
      {
        pattern: new RegExp('^[0-9]+[.]?[0-9]{0,' + num + '}$'),
        message: `输入最多${num}位小数`,
        trigger: 'blur',
      },
    ];
  },
  //姓名
  realName: (required = false) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        max: 15,
        message: `不能大于15位字符！`,
        trigger: 'blur',
      },
      {
        pattern: new RegExp(
          '^(([a-zA-Z+\\.?\\·?a-zA-Z+]{2,15}$)|([\u4e00-\u9fa5+\\·?\u4e00-\u9fa5+]{2,15}$))',
        ),
        message: `请输入正确姓名!`,
        trigger: 'blur',
      },
    ];
  },
  //身份证
  IDcode: (required = false) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        pattern:
          /^(^[1-9]\d{5}[1-9]\d{3}(((0[2])([0|1|2][0-8])|(([0-1][1|4|6|9])([0|1|2][0-9]|[3][0]))|(((0[1|3|5|7|8])|(1[0|2]))(([0|1|2]\d)|3[0-1]))))((\d{4})|\d{3}[Xx])$)$/,
        message: `请输入正确身份证号!`,
        trigger: 'blur',
      },
    ];
  },
  //手机号
  phone: (required = false) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        pattern:
          /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/,
        message: `请输入正确手机号!`,
        trigger: 'blur',
      },
    ];
  },
  //密码
  password: (required = false, min = 8, max = 18) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        pattern: new RegExp(
          `^(?=.*?[a-zA-Z])(?=.*?[0-9])(?=.*?[_\\-@$#*^%&~!=])[a-zA-Z0-9_\\-@$#*^%&~!=]{${min},${max}}$`,
        ),
        message: `密码必须由字母、数字和特殊字符三种组成的${min}-${max}位字符`,
        trigger: 'blur',
      },
    ];
  },
  // 邮箱
  email: (required = false) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        max: 50,
        message: `不能大于50位字符！`,
        trigger: 'blur',
      },
      {
        pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
        message: `请输入正确的电子邮箱!`,
        trigger: 'blur',
      },
    ];
  },
  // 社会统一信用代码
  societyCode: (required = false) => {
    return [
      {
        required: required,
        message: '不能为空!',
        trigger: ['blur'],
      },
      {
        pattern: /[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/,
        message: `请输入正确社会统一信用代码!`,
        trigger: 'blur',
      },
    ];
  },

  checkValidate: (formRef) => {
    return new Promise((resolve, reject) => {
      formRef.validate((valid) => {
        if (valid) {
          resolve(valid);
        } else {
          reject(valid);
        }
      });
    });
  },
};

export default validate;

export const fieldsValidate = (values, rules, scope) => {
  let flag = true;
  Object.keys(rules).forEach((key) => {
    rules[key].forEach((field) => {
      if (!flag) {
        return;
      }
      // 必填
      if (
        field.required &&
        (values[key] === null ||
          values[key] === undefined ||
          values[key] === '')
      ) {
        flag = false;
        scope.$message.error(field.text);
        return;
      }
      // 长度
      if (
        field.maxLength &&
        values[key] &&
        `${values[key]}`.length > field.maxLength
      ) {
        flag = false;
        scope.$message.error(field.text);
        return;
      }
      if (
        field.pattern &&
        values[key] !== undefined &&
        values[key] !== '' &&
        values[key] !== null &&
        !field.pattern.test(values[key])
      ) {
        flag = false;
        scope.$message.error(field.text);
        return;
      }
    });
    if (!flag) {
      return;
    }
  });
  return flag;
};
